package com.da.lon.wang.xlg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.AddressListAdapter;
import com.da.lon.wang.xlg.bean.AddressList;
import com.da.lon.wang.xlg.bean.AddressListBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.Mode;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;
import java.util.List;

public class AddressActivity extends BaseActivity implements
        OnRefreshListener2<ListView>, OnClickListener {

    private PullToRefreshListView listView;
    private String id;
    private int page;

    private HttpLoader<AddressListBean> loader;
    private List<AddressList> list = new ArrayList<AddressList>();

    private ImageButton backButton;
    private TextView fixView;

    private Context context;

    private int resultCode = 0;
    private String addid = "";

    private AddressListAdapter adapter;
    private String pro_type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ouka_address);
        setBasicUI();
        context = this;

        backButton = (ImageButton) findViewById(R.id.ouka_address_left);
        fixView = (TextView) findViewById(R.id.ouka_address_right);

        backButton.setOnClickListener(this);
        fixView.setOnClickListener(this);

        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            addid = extras.getString("addid");
            pro_type = extras.getString("pro_type");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initView();
    }

    private void initView() {

        listView = (PullToRefreshListView) findViewById(R.id.ouka_address_listview);
        listView.setMode(Mode.BOTH);
        listView.setOnRefreshListener(this);

        adapter = new AddressListAdapter(list, context, addid);
        listView.getRefreshableView().setAdapter(adapter);

        id = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.USER_ID, "");

        listView.getRefreshableView().setOnItemClickListener(
                new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent,
                                            View view, int position, long id) {

                        AddressList bean = (AddressList) parent
                                .getAdapter().getItem(position);

                        Intent intent = new Intent();

                        intent.putExtra("addid", bean.getAdd_id());
                        setResult(resultCode, intent);
                        finish();

                    }
                });

        page = 1;

        loader = new HttpLoader<AddressListBean>(this, XLGConstants.URL
                + "Shipping-addresslist", AddressListBean.class, 60 * 1000);
        AjaxParams params = new AjaxParams();
        params.put("page", page + "");
        params.put("id", id);
        params.put("token", XLGDataUtils.encrypt2MD5("Shipping" + "2015#$%"
                + "addresslist"));

        loader.setManagerListener(new OnWebLoadListener<AddressListBean>() {

            @Override
            public void OnStart() {
                showDialog();
            }

            @Override
            public void OnError(String error) {
                dismissDialog();
            }

            @Override
            public void onSuccess(AddressListBean t) {
                dismissDialog();

                if (t != null && "000000".equals(t.getCode())) {

                    if (t.getResult() == null) {
                        list.clear();
                        adapter.notifyDataSetChanged();
                    } else {
                        list.clear();
                        list.addAll(t.getResult());
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
        loader.loadData(params);


    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

        page = 1;

        loader = new HttpLoader<>(this, XLGConstants.URL
                + "Shipping-addresslist", AddressListBean.class, 60 * 1000);
        AjaxParams params = new AjaxParams();
        params.put("page", page + "");
        params.put("id", id);
        params.put("token", XLGDataUtils.encrypt2MD5("Shipping" + "2015#$%"
                + "addresslist"));

        loader.setManagerListener(new OnWebLoadListener<AddressListBean>() {

            @Override
            public void OnStart() {
                showDialog();
            }

            @Override
            public void OnError(String error) {
                dismissDialog();
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                });
            }

            @Override
            public void onSuccess(AddressListBean t) {
                dismissDialog();
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                });

                if (t != null && "000000".equals(t.getCode())) {
                    if (t.getResult() == null) {
                        list.clear();
                        adapter.notifyDataSetChanged();
                    } else {
                        list.clear();
                        list.addAll(t.getResult());
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
        loader.loadData(params);


    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        loader = new HttpLoader<AddressListBean>(this, XLGConstants.URL
                + "Shipping-addresslist", AddressListBean.class, 60 * 1000);
        AjaxParams params = new AjaxParams();
        params.put("page", page + 1 + "");
        params.put("id", id);
        params.put("token", XLGDataUtils.encrypt2MD5("Shipping" + "2015#$%"
                + "addresslist"));

        loader.setManagerListener(new OnWebLoadListener<AddressListBean>() {

            @Override
            public void OnStart() {
                showDialog();
            }

            @Override
            public void OnError(String error) {
                dismissDialog();
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                });
            }

            @Override
            public void onSuccess(AddressListBean t) {
                dismissDialog();

                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                });

                if (t != null && "000000".equals(t.getCode())) {

                    if (t.getResult() != null && t.getResult().size() != 0) {
                        page = page + 1;
                        list.addAll(t.getResult());
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
        loader.loadData(params);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ouka_address_left:
                finish();
                break;

            case R.id.ouka_address_right:
                Intent intent = new Intent(AddressActivity.this,
                        FixAddressActivity.class);
                intent.putExtra("pro_type", pro_type);
                startActivity(intent);
                break;

            default:
                break;
        }

    }

}
